﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Application.Common.Models;
using wt.wine.Application.ManageSkutable;
using wt.wine.Domain.Entities;

namespace wt.wine.Infrastructure.Persistence.Repository
{
    public class skutableRepository : IskutableRepository
    {
        private readonly IApplicationDbContext _dbcontext;
        public skutableRepository(IApplicationDbContext dbcontext)
        {
            _dbcontext = dbcontext;
        }

        public async Task<skutable> GetSkuTableById(int id, CancellationToken cancellationToken)
        {
            return await _dbcontext.skutable.AsNoTracking().Where(p => p.id == id && p.state >= 0).FirstOrDefaultAsync(cancellationToken);
        }

        /// <summary>
        /// 通过登录id获取商品信息
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<List<GetSkutableNameByUserIdDTO>> GetSkutableNameListByUserId(int userid, CancellationToken cancellationToken)
        {
            return await _dbcontext.skutable.AsNoTracking().Where(p => p.userid == userid).Select(p => new GetSkutableNameByUserIdDTO() { id = p.id, name = p.name }).ToListAsync(cancellationToken);
        }

        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="model"></param>
        /// <param name="cancellation"></param>
        /// <returns></returns>
        public async Task AddSkutable(skutable model, CancellationToken cancellation)
        {
            await _dbcontext.skutable.AddAsync(model, cancellation);
            await _dbcontext.SaveChangesAsync(cancellation);
        }

        /// <summary>
        /// 通过商品id 获取需要修改的商品
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userid"></param>
        /// <param name="cancecllationtoken"></param>
        /// <returns></returns>
        public async Task<skutable> GetSkutableById(int id, int userid, CancellationToken cancecllationtoken)
        {
            return await _dbcontext.skutable.AsNoTracking().Where(p => p.id == id && p.userpid == userid && p.state >= 0).FirstOrDefaultAsync(cancecllationtoken);
        }

        /// <summary>
        /// 修改商品信息
        /// </summary>
        /// <param name="model"></param>
        /// <param name="cancellationtoken"></param>
        /// <returns></returns>
        public async Task EditSkutable(skutable model, CancellationToken cancellationtoken)
        {
            _dbcontext.Entry<skutable>(model).Property("name").IsModified = true;
            _dbcontext.Entry<skutable>(model).Property("price").IsModified = true;
            _dbcontext.Entry<skutable>(model).Property("unit").IsModified = true;
            _dbcontext.Entry<skutable>(model).Property("userpid").IsModified = true;
            _dbcontext.Entry<skutable>(model).Property("updatetime").IsModified = true;
            _dbcontext.Entry<skutable>(model).Property("remark").IsModified = true;
            await _dbcontext.SaveChangesAsync(cancellationtoken);
        }

        /// <summary>
        /// 【经销商】产品列表管理
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="cancellationtoken"></param>
        /// <returns></returns>
        public async Task<PaginatedList<GetSkutableListDTO>> GetSkuTableList(int userid, int pageIndex, int pageSize, CancellationToken cancellationtoken)
        {
            var query = (from s in _dbcontext.skutable.AsNoTracking()
                         where s.userpid == userid && s.state >= 0
                         orderby s.updatetime descending
                         select new GetSkutableListDTO()
                         {
                             id = s.id,
                             name = s.name,
                             price = s.price,
                             remark = s.remark,
                             unit = s.unit
                         }
                         );
            return await PaginatedList<GetSkutableListDTO>.CreateAsync(query, pageIndex, pageSize, cancellationtoken);
        }


        /// <summary>
        /// 设备 加酒 获取商品列表
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="cancellationtoken"></param>
        /// <returns></returns>
        public async Task<PaginatedList<GetSkuTableNameAddWineListDTO>> GetProductNameByAddWine(int userid, int pageIndex, int pageSize, CancellationToken cancellationtoken)
        {

            var query =(from s in _dbcontext.skutable.AsNoTracking().Where(p => p.userpid == userid && p.state >= 0)
                         orderby s.updatetime descending
                         select new GetSkuTableNameAddWineListDTO()
                         {
                             id = s.id,
                             name = s.name
                         });
            return await PaginatedList<GetSkuTableNameAddWineListDTO>.CreateAsync(query, pageIndex, pageSize, cancellationtoken);
        }
    }
}
